// pages/home/home.js
const app = getApp();

Page({
  data: {
    currentTab: 'home',
    homeList:{
      battery:null,
      chargeStatus:null,
      duration:null,
      heights:[],
      posture:'未知',
    },
    sleepData: null,
    pillowStatus: {
      temperature: 24,
      humidity: 45,
      firmness: 7
    },
    recentSleepTrend: [65, 70, 75, 80, 85, 82, 85],
  },

  onLoad: function (options) {
    this.updateUserInfo();
    this.getSleepData();
  },

  onShow: function () {
    this.getSleepData();
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: this.data.currentTab
      })
    }
  },
  init(cmd, list) {
    if(cmd=='RED:01'){
      console.log('list',list);
      this.setData({
        homeList:list,
      },()=>{
       let batteryProgress= this.selectComponent('#batteryProgress');
       batteryProgress.init(list);
      });
    }
  },
  onGlobalDataUpdate: function () {
    this.getSleepData();
  },

  updateUserInfo: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      });
    } else if (app.userInfoReadyCallback) {
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        });
      };
    }
  },

  getSleepData: function () {
    if (app.globalData.sleepData) {
      this.setData({
        sleepData: app.globalData.sleepData.dailyData
      });
    } else {
      app.fetchSleepData();
    }
  },
  // 学习睡姿
  postureFn(){
    this.triggerEvent('postureFn');
  },
  // 设置枕头
  pillowSetFn(){
    this.triggerEvent('pillowSetFn');
  },
});